package class202204._21_25;

import java.util.Arrays;

public class demo20220422 {
    /**
     * 数组二分查找
     * @param array
     * @param key
     * @return
     */
    public static int binarysearch(int[] array,int key) {
        int left = 0;
        int right = array.length-1;
        while (left<=right){
            int mid = (left+right)>>>1;
            if (array[mid] < key) {
                left = mid + 1;
            }else if (array[mid] == key){
                    return mid;
            }else {
                    right=mid-1;
            }
        }
        return -1;
    }

    public static void main1(String[] args) {
        int[] array = {1,6,9,11,16,17,19,36};
        int ret = binarysearch(array,17);
        if (ret==-1){
            System.out.println("没有你所要找的关键字！");
        }else {
            System.out.println("你所要找的下标为：" + ret);
        }
    }

    /**
     * 冒泡排序
     * @param array
     */
    public static void bubblestore(int[] array) {
        for (int i = 0; i < array.length-1; i++) {
            boolean flg = false;
            for (int j = 0; j < array.length-1-i; j++) {
                while (array[j]>array[j+1]){
                    int till = array[j+1];
                    array[j+1] =array[j];
                    array[j]=till;
                    flg = true;
                }
            }
            if (flg == false);
            break;
        }
    }

    public static void main2(String[] args) {
        int[] array = {23,4,52,41,32,19,9,66};
        System.out.println("没排序之前的数组：" + Arrays.toString(array));
        bubblestore(array);
        System.out.println("排序之后的数组：" + Arrays.toString(array));
    }

    /**
     * 判断数组是否有序
     * @param args
     */
    public static void main3(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        int[] arr1 = {1, 3, 2, 4, 5};
        stor(arr);
        stor(arr1);
    }
    public static void stor(int[] arr){
        boolean flg = false;
        for (int i = 0; i < arr.length-1; i++) {
            if (arr[i]>arr[i+1]){
                flg = true;
            }
        }if(flg == false){
            System.out.println("此数组有序！");
        }else {
            System.out.println("此数组无序！");
        }
    }
}
